package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.common.utils.StringUtils;
import com.learn.core.domain.*;
import com.learn.core.mapper.*;
import com.learn.core.service.ILExamPaperService;
import com.learn.framework.service.impl.BaseServiceImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LExamPaperServiceImpl
 * @date 2021.03.25 11:07
 */
@Service
public class LExamPaperServiceImpl extends BaseServiceImpl<LExamPaperMapper, LExamPaper> implements ILExamPaperService {
    @Autowired
    LExtractQuestionsMapper lExtractQuestionsMapper;
    @Autowired
    LQuestionsMapper lQuestionsMapper;
    @Autowired
    LExamMapper lExamMapper;
    @Autowired
    LExamPaperMapper lExamPaperMapper;
    @Autowired
    LQuestionsSortMapper lQuestionsSortMapper;

    @Override
    public void examPaper(Integer id,String userId){
        LExam exam = lExamMapper.selectById(id);

        // 删除原生成试卷
        QueryWrapper<LExamPaper> queryWrapper = new QueryWrapper();
        queryWrapper.eq("exam_id", id);
        lExamPaperMapper.delete(queryWrapper);

        // 生成制定条数时间
        for (int i = 1; i <= exam.getRandomSeveral();i++) {
            //TODO 根据考次规定生成试卷
            QueryWrapper<LExtractQuestions> lExtractQuestionsQueryWrapper = new QueryWrapper<LExtractQuestions>();
            lExtractQuestionsQueryWrapper.eq("exam_id", id);
            List<LExtractQuestions> list = lExtractQuestionsMapper.selectList(lExtractQuestionsQueryWrapper);
            Map<String, Object> map = new HashMap<>();
            for (LExtractQuestions lExtractQuestions : list) {
                if (1 == lExtractQuestions.getQuestionsTypeId()) {
                    map.put("radio", examQuestionsList(exam,lExtractQuestions.getNumber(),lExtractQuestions.getQuestionsTypeId()));
                } else if (2 == lExtractQuestions.getQuestionsTypeId()) {
                    map.put("check", examQuestionsList(exam,lExtractQuestions.getNumber(),lExtractQuestions.getQuestionsTypeId()));
                } else if (3 == lExtractQuestions.getQuestionsTypeId()) {
                    map.put("judge", examQuestionsList(exam,lExtractQuestions.getNumber(),lExtractQuestions.getQuestionsTypeId()));
                }
            }
            JSONObject jsonObject = JSONObject.fromObject(map);

            // 生产试卷
            LExamPaper lExamPaper = new LExamPaper();
            lExamPaper.setExamId(id);
            lExamPaper.setExamQuestionsId(jsonObject.toString());
            lExamPaper.setCreateTime(new Date());
            lExamPaper.setCreateUser(userId);
            lExamPaperMapper.insert(lExamPaper);
        }
    }

    private List<Integer> examQuestionsList(LExam exam,Integer number,Integer questionsTypeId){
        List<Integer> stringList = new ArrayList<>();
        QueryWrapper<LQuestions> lQuestionsQueryWrapper = new QueryWrapper<LQuestions>();
        lQuestionsQueryWrapper.eq("questions_type",questionsTypeId);
        if(StringUtils.isNotEmpty(exam.getKnowledge()))
            lQuestionsQueryWrapper.like("knowledge",exam.getKnowledge());
        if(StringUtils.isNotEmpty(exam.getDifficulty()))
            lQuestionsQueryWrapper.eq("questions_difficulty",exam.getDifficulty());
        if(StringUtils.isNotEmpty(exam.getQuestionsSortId())) {
            // 题库分类下所有题 包括子分类
            LQuestionsSort lQuestionsSort = lQuestionsSortMapper.selectById(exam.getQuestionsSortId());
            List<Integer> ids = new ArrayList<>();
            ids.add(lQuestionsSort.getQuestionsSortId());
            List<Integer> sortIds = nodeStr(ids);
            lQuestionsQueryWrapper.in("questions_sort_id", sortIds);
        }
        lQuestionsQueryWrapper.orderByAsc("rand()");
        lQuestionsQueryWrapper.last(" limit " + number);
        List<LQuestions> lQuestionsList = lQuestionsMapper.selectList(lQuestionsQueryWrapper);
        for (LQuestions lQuestions : lQuestionsList) {
            stringList.add(lQuestions.getQuestionsId());
        }
        return stringList;
    }

    //子分类ID
    private List<Integer> nodeStr (List<Integer> lists){
        List<Integer> ids = new ArrayList<>();
        QueryWrapper<LQuestionsSort> queryWrapper = new QueryWrapper<LQuestionsSort>();
        for (int i = 0; i <lists.size() ; i++) {
            int orgid = lists.get(i);

            queryWrapper.eq("parent_node",orgid);
            List<LQuestionsSort> lQuestionsSorts = lQuestionsSortMapper.selectList(queryWrapper);
            for (int j = 0; j < lQuestionsSorts.size(); j++) {
                LQuestionsSort lQuestionsSort = lQuestionsSorts.get(j);
                ids.add(lQuestionsSort.getQuestionsSortId());
            }
        }
        if(ids.size() > 0){
            this.nodeStr(ids);
        }
        ids.addAll(lists);
        return ids;
    }
}
